﻿using OrderSystem.Common;
using OrderSystem.Templet;
using System;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryShatters : FormTemplet
    {
        public FormQueryShatters()
        {
            this.InitializeComponent();
        }

        private void FormQueryOrders_Load(object sender, EventArgs e)
        {
            this.dtpDateStart.Enabled = false;
            this.dtpDateEnd.Enabled = false;
            this.chkLockedData.Enabled = false;
        }

        private void chkDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkDateUsed.Checked)
            {
                this.dtpDateStart.Enabled = true;
                this.dtpDateEnd.Enabled = true;
            }
            else
            {
                this.dtpDateStart.Enabled = false;
                this.dtpDateEnd.Enabled = false;
            }
        }

        private void chkLockedDataUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkLockedDataUsed.Checked)
            {
                this.chkLockedData.Enabled = true;
            }
            else
            {
                this.chkLockedData.Enabled = false;
            }
        }

        private void btnShowSumUp_Click(object sender, EventArgs e)
        {
            ShatterCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormShatterSumUp formShatterSumUp = new FormShatterSumUp(conditionForQuery);
                formShatterSumUp.MdiParent = base.MdiParent;
                formShatterSumUp.Show();
                formShatterSumUp.Activate();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private ShatterCondition GetConditionForQuery()
        {
            ShatterCondition shatterCondition = new ShatterCondition();
            ShatterCondition result;
            if (!this.chkDateUsed.Checked && !this.chkLockedDataUsed.Checked && this.txtShatterNumber.Text.Trim() == "" && this.txtRemark.Text.Trim() == "")
            {
                MessageBox.Show("至少要输入一条查询信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                result = null;
            }
            else
            {
                if (this.chkDateUsed.Checked)
                {
                    int num = DateTime.Parse(this.dtpDateStart.Value.ToShortDateString() + " 00:00:00").CompareTo(DateTime.Parse(this.dtpDateEnd.Value.ToShortDateString() + " 23:59:59"));
                    if (num > 0)
                    {
                        MessageBox.Show("订购日期：后面的日期要大于或等于前面的日期", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpDateEnd.Focus();
                        result = null;
                        return result;
                    }
                    shatterCondition.DateStart = this.dtpDateStart.Value.ToShortDateString() + " 00:00:00";
                    shatterCondition.DateEnd = this.dtpDateEnd.Value.ToShortDateString() + " 23:59:59";
                }
                if (this.chkLockedDataUsed.Checked)
                {
                    if (this.chkLockedData.Checked)
                    {
                        shatterCondition.Locked = 1;
                    }
                    else
                    {
                        shatterCondition.Locked = 0;
                    }
                }
                if (this.txtShatterNumber.Text.Trim() != "")
                {
                    shatterCondition.ShatterNumber = this.txtShatterNumber.Text.Trim();
                }
                if (this.txtRemark.Text.Trim() != "")
                {
                    if (this.txtRemark.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("字符数量不能大于64", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtRemark.Focus();
                        result = null;
                        return result;
                    }
                    shatterCondition.Remark = this.txtRemark.Text.Trim();
                }
                result = shatterCondition;
            }
            return result;
        }
    }
}
